Skip to content

SoundWire: fix pm_runtime usage - #2530

Merged
plbossart merged 8 commits into
thesofproject:topic/sof-devfrom
plbossart:sdw/fix-pm-runtime
Nov 5, 2020
Merged

plbossart merged 8 commits into
thesofproject:topic/sof-devfrom
plbossart:sdw/fix-pm-runtime

Conversation

@plbossart

Copy link
Copy Markdown
Member

@bardliao reported a weird behavior in #2521, and suggested a solution in #2526 to make sure resuming a Slave device waits for the bus to be resumed and the enumeration/initialization to complete.

That didn't seem quite right, and after a couple of hours I found major issues leading to timeouts in our solution. Those issues may explain some of the suspend-resume runtime issues we've been seeing randomly for several months now?

There may be other issues hidden, please take the time to look at drivers/soundwire/bus.c and see if you find other problematic cases.

@plbossart

Copy link
Copy Markdown
Member Author

@fredoh9 is there a way you can start a manual daily test on this PR before the week-end?

@fredoh9

fredoh9 commented Oct 23, 2020

Copy link
Copy Markdown
Collaborator

@fredoh9 is there a way you can start a manual daily test on this PR before the week-end?

Yes there is a easy way to run daily test. I will trigger this and let you know.

@plbossart

plbossart commented Oct 24, 2020 •

Copy link
Copy Markdown
Member Author

@bardliao I found additional problems... The use of regcache in the resume routine was probably broken, and the master device didn't seem to have the right pm_runtime settings either.

With these changes pm_runtime seems to be fine, but somehow the second system suspend with rtcwake is fatal -> cannot resume. I must have missed something. Not sure why the first suspend-resume works. @ranj063 does this ring a bell?

I never suspected we had so many issues in the code...

Comment thread drivers/soundwire/bus.c
ret = pm_runtime_get_sync(&slave->dev);
if (ret < 0 && ret != -EACCES) {
pm_runtime_put_noidle(slave->bus->dev);
pm_runtime_put_noidle(&slave->dev);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart I don't know if it is a right change. As you said in #2521, there is a dead lock between regcache_cache_only and regmap_read. The codec driver's logic is that it doesn't expect any IO access in suspend even the register is volatile. And that's why we set cache only in suspend and sync cache in resume. We will get XX when we try to read a register which is unavailable and it is a normal behavior of regmap. Codec driver should resume codec device before regmap_read if it want to read a volatile register in suspend.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's absolutely the right change. We should NOT deal with the parent when playing with the codec. That is a recipe for not waiting for the resume to complete.

I agree there is a conceptual issue with regmap but hiding it by changing the power state at a higher level is completely wrong. We've done this before and it was really bad.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't have the full soundwire context, but I agree this seems like the right thing and is aligned with HDA (e.g. in hdac_device.c we do not power up/down the bus, but rather the device).

pm_runtime_mark_last_busy(&bus->md->dev);
pm_runtime_set_active(&bus->md->dev);
pm_runtime_enable(&bus->md->dev);
pm_runtime_idle(&bus->md->dev);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I understand it correctly, that with this patch the problem, described in the commit message, doesn't occur any more, because in those cases, when rt-PM is trying to activate a child, now the parent isn't yet suspended because of the autosuspend delay?.. If that's correct, it doesn't seem right to me, relying on autosuspend to keep the parent on while we (re-)activate a child?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the point is that we let the parent be part of the pm_runtime flow.
When we activate a child, the parent will be activated by construction. Likewise, when the child is no longer active, the parent will be suspended as well.

This initial pm_runtime_idle is just to make sure the device becomes suspended IF its children are also suspended. Otherwise we've set it as active initially and it remains active forever.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my question wasn't to that pm_runtime_idle() call but to the complete code block, rather to the autosuspend handling. Whether setting parent's autosuspend timeout we make sure it's still active in the beginning of the runtime suspend process?
BTW, in the commit message you probably meant "Force the parent device"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @lyakh I don't understand the objection or question.

You can only enter the pm_runtime suspend routine if the device is pm_runtime_active. that's by design. We use the same sequence in Ranjani's probes and audio client patches. It's not new, just the same recipe applied here.

@lyakh lyakh Oct 28, 2020 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message says, that this patch fixes the

runtime PM trying to activate child device sdw:3:25d:715:0 but parent (sdw-master-3) is not active

error message by "forcing the parent device to be active and using autosuspend to avoid these warnings." That makes me think, that without this patch the parent has already suspended when the driver is trying to activate the child. And with this patch the parent isn't yet suspended at that time, because it's still in its autosuspend delay?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For PM runtime, a child can only become active when its parents are also active, and conversely a parent can only suspend when all of its children are suspended.

This sdw-master-N parent is an artificial layer we don't really needed, so we are just making it 'transparent' from the pm_runtime perspective, so that the codec device and the Intel master interface are connected and pm_runtime behaves as expected.

The first part is to make sure the device becomes active initially. If you don't no one will ever request the device to be active, and you get those warnings. By setting it to active, you allow the children to become active as well.

The autosuspend is the dual operation. You allow this device to suspend when no children are active.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart so the error message appears not because the parent got suspended but because it wasn't activated to begin with? Got it, thanks!

@ranj063

ranj063 commented Oct 26, 2020

Copy link
Copy Markdown
Collaborator

@bardliao I found additional problems... The use of regcache in the resume routine was probably broken, and the master device didn't seem to have the right pm_runtime settings either.

With these changes pm_runtime seems to be fine, but somehow the second system suspend with rtcwake is fatal -> cannot resume. I must have missed something. Not sure why the first suspend-resume works. @ranj063 does this ring a bell?

I never suspected we had so many issues in the code...

@plbossart we had similar issues with subsequent system suspend/resume because of imbalances in the sequences for suspend/resume in the past. Do you have the log error for the failure?

@plbossart

Copy link
Copy Markdown
Member Author

@bardliao I found additional problems... The use of regcache in the resume routine was probably broken, and the master device didn't seem to have the right pm_runtime settings either.
With these changes pm_runtime seems to be fine, but somehow the second system suspend with rtcwake is fatal -> cannot resume. I must have missed something. Not sure why the first suspend-resume works. @ranj063 does this ring a bell?
I never suspected we had so many issues in the code...

@plbossart we had similar issues with subsequent system suspend/resume because of imbalances in the sequences for suspend/resume in the past. Do you have the log error for the failure?

No. the device is stuck on the second suspend and I lose all connections. Hard reboot by pressing the power button is the only way out.

Isn't there a way to simulate a suspend/resume at a device level? I am pretty sure I've seen something on this before.

@ranj063

ranj063 commented Oct 26, 2020

Copy link
Copy Markdown
Collaborator

@bardliao I found additional problems... The use of regcache in the resume routine was probably broken, and the master device didn't seem to have the right pm_runtime settings either.
With these changes pm_runtime seems to be fine, but somehow the second system suspend with rtcwake is fatal -> cannot resume. I must have missed something. Not sure why the first suspend-resume works. @ranj063 does this ring a bell?
I never suspected we had so many issues in the code...

@plbossart we had similar issues with subsequent system suspend/resume because of imbalances in the sequences for suspend/resume in the past. Do you have the log error for the failure?

No. the device is stuck on the second suspend and I lose all connections. Hard reboot by pressing the power button is the only way out.

Isn't there a way to simulate a suspend/resume at a device level? I am pretty sure I've seen something on this before.

Not really, Pierre. I had a crude way to do this using a temporary debugfs entry to call the suspend/resume callbacks from userspace. Let me bring up my system in the next couple of hours and I can help debug this.

@plbossart

plbossart commented Oct 26, 2020 •

Copy link
Copy Markdown
Member Author

@ranj063 the issue I had was with rtcwake. When I use the information provided by Ubuntu (https://wiki.ubuntu.com/DebuggingKernelSuspend)

and run

apt install pm-utils
sync && echo 1 > /sys/power/pm_trace && pm-suspend"

then I can suspend-resume as much as I want. Weird.

Update: I can reproduce the issue, this happens when I try the 'platform' level on the second test.

root@plb:~# echo freezer > /sys/power/pm_test
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# echo devices > /sys/power/pm_test
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# echo platform > /sys/power/pm_test
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
>>>> stuck here

@plbossart

Copy link
Copy Markdown
Member Author

@ranj063 the issue I had was with rtcwake. When I use the information provided by Ubuntu (https://wiki.ubuntu.com/DebuggingKernelSuspend)

and run

apt install pm-utils
sync && echo 1 > /sys/power/pm_trace && pm-suspend"

then I can suspend-resume as much as I want. Weird.

Update: I can reproduce the issue, this happens when I try the 'platform' level on the second test.

root@plb:~# echo freezer > /sys/power/pm_test
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# echo devices > /sys/power/pm_test
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# echo platform > /sys/power/pm_test
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
root@plb:~# sync && echo 1 > /sys/power/pm_trace && pm-suspend
>>>> stuck here

Update2: turns out the first resume never completes, so the second is stuck.... Debugging.

@plbossart

Copy link
Copy Markdown
Member Author

@bardliao @ranj063 can you take a look at the hack in the second patch and let me know if this looks ok? My brain is fried but I can't think of a race condition for now. What I wanted to do is only resume if the device is suspended, and since we don't have an API to do this I did it in two steps.

Comment thread drivers/soundwire/bus.c Outdated
pm_runtime_get_noresume(&slave->dev);

if (pm_runtime_status_suspended(&slave->dev)) {
ret = pm_runtime_resume(&slave->dev);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart just out of cuioristy why not just pm_runtime_get_sync() here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranj063 because we can't call pm_runtime_get_sync() from the resume routine. that leads to a deadlock.

@bardliao

Copy link
Copy Markdown
Collaborator

@plbossart Similar to soundwire: bus: use no_pm IO routines for all interrupt handling, should we use no_pm for regmap read/write functions?
I found an issue when a jack is plugged in or out. Codec will be attached when an interrupt is triggered, and codec driver will initialize the codec with regmap functions in its update_status function. initialization_complete will be completed after that.
However, codec's resume function will wait for initialization_complete. So codec's resume function will return -ETIMEDOUT if we try to resume codec in regmap read/write functions.
0001-soundwire-regmap-use-_no_pm-functions-in-regmap_read.patch

@bardliao

Copy link
Copy Markdown
Collaborator

Actually, we don't need soundwire: bus: allow for read/write from resume routines if we have oundwire/regmap: use _no_pm functions in regmap_read/write. I can run "sudo rtcwake -m mem -s 5" many times without soundwire: bus: allow for read/write from resume routines, but with oundwire/regmap: use _no_pm functions in regmap_read/write. I don't think waking up the target device is a part of regmap's job. It can just return error if it can't access the target device.

@bardliao

bardliao commented Oct 27, 2020 •

Copy link
Copy Markdown
Collaborator

Actually, we don't need soundwire: bus: allow for read/write from resume routines if we have oundwire/regmap: use _no_pm functions in regmap_read/write. I can run "sudo rtcwake -m mem -s 5" many times without soundwire: bus: allow for read/write from resume routines, but with oundwire/regmap: use _no_pm functions in regmap_read/write. I don't think waking up the target device is a part of regmap's job. It can just return error if it can't access the target device.

Sorry, I was wrong. We do need soundwire: bus: allow for read/write from resume routines. Playback doesn't work without soundwire: bus: allow for read/write from resume routines

@plbossart

Copy link
Copy Markdown
Member Author

@plbossart Similar to soundwire: bus: use no_pm IO routines for all interrupt handling, should we use no_pm for regmap read/write functions?
I found an issue when a jack is plugged in or out. Codec will be attached when an interrupt is triggered, and codec driver will initialize the codec with regmap functions in its update_status function. initialization_complete will be completed after that.
However, codec's resume function will wait for initialization_complete. So codec's resume function will return -ETIMEDOUT if we try to resume codec in regmap read/write functions.
0001-soundwire-regmap-use-_no_pm-functions-in-regmap_read.patch

@bardliao can you elaborate on the scenario that fails? We do use pm_runtime_get_sync() on an alert, so not sure which path you are referring to.

I thought about your suggestion to use no_pm routines from regmap, but we don't really control when regmap will be used.
For example, if you play on the headset link, but the speaker is suspended. Can we guarantee that the register access through regmap will never result in a bus access?

@bardliao

Copy link
Copy Markdown
Collaborator

@plbossart Similar to soundwire: bus: use no_pm IO routines for all interrupt handling, should we use no_pm for regmap read/write functions?
I found an issue when a jack is plugged in or out. Codec will be attached when an interrupt is triggered, and codec driver will initialize the codec with regmap functions in its update_status function. initialization_complete will be completed after that.
However, codec's resume function will wait for initialization_complete. So codec's resume function will return -ETIMEDOUT if we try to resume codec in regmap read/write functions.
0001-soundwire-regmap-use-_no_pm-functions-in-regmap_read.patch

@bardliao can you elaborate on the scenario that fails? We do use pm_runtime_get_sync() on an alert, so not sure which path you are referring to.

When a jack is inserted or removed, the codec will be attached firstly. And the problem happens in sdw_handle_slave_status()
The flow is

sdw_handle_slave_status
    -> sdw_update_slave_status 
         ->  slave->ops->update_status (rt711_sdca_update_status) 
                -> 
			regcache_cache_only(rt711->regmap, false);
                        regcache_cache_bypass(rt711->regmap, true);
                        regmap_write(rt711->regmap, SDW_SCP_SDCA_INTMASK1,
                                SDW_SCP_SDCA_INTMASK_SDCA_0);  <<<<< We try to resume the codec here
                        regmap_write(rt711->regmap, SDW_SCP_SDCA_INTMASK2,
                                SDW_SCP_SDCA_INTMASK_SDCA_8);
                        regcache_cache_bypass(rt711->regmap, false);

    ->  complete(&slave->initialization_complete); <<<<< initialization_complete complete here

rt711_sdca_dev_resume <<<<< This is invoked by rt711_sdca_update_status() -> regmap_write()
    -> time = wait_for_completion_timeout(&slave->initialization_complete,
                                msecs_to_jiffies(RT711_PROBE_TIMEOUT)); <<<<< We wait for initialization_complete here, but initialization_complete complete after sdw_update_slave_status(). So rt711_sdca_dev_resume() will return  -ETIMEDOUT;

Please see the dmesg log when regmap uses sdw_write and sdw_write_no_pm

initialization not complete, timed out log
[   26.162505] intel-sdw intel-sdw.0: intel_link_power_up: powering up all links
[   26.162509] intel-sdw intel-sdw.0: intel_link_power_up: first link up, programming SYNCPRD
[   26.162854] rt711-sdca sdw:0:25d:711:1: sdw_modify_slave_status: initializing completion for Slave 1
[   26.164192] soundwire sdw-master-0: Slave attached, programming device number
[   26.164357] soundwire sdw-master-0: SDW Slave Addr: 30025d071101
[   26.164358] soundwire sdw-master-0: SDW Slave class_id 1, part_id 711, mfg_id 25d, unique_id 0, version 3
[   26.164359] soundwire sdw-master-0: Slave already registered, reusing dev_num:1
[   26.164590] soundwire sdw-master-0: No more devices to enumerate
[   26.164616] rt711-sdca sdw:0:25d:711:1: sdw_modify_slave_status: signaling completion for Slave 1
[   26.164795] rt711-sdca sdw:0:25d:711:1: Configured bus base 1, scale 3, mclk 19200000, curr_freq 4800000
[   26.164914] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_update_status status 1
[   26.164919] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_dev_resume
[   28.189918] rt711-sdca sdw:0:25d:711:1: Initialization not complete, timed out
[   28.189936] rt711-sdca sdw:0:25d:711:1: bard: sdw_handle_slave_status initialization_complete complete
[   29.746128] intel-sdw intel-sdw.0: intel_link_power_down: powering down all links
no error log
[   58.468913] intel-sdw intel-sdw.0: intel_link_power_up: powering up all links
[   58.468917] intel-sdw intel-sdw.0: intel_link_power_up: first link up, programming SYNCPRD
[   58.469294] rt711-sdca sdw:0:25d:711:1: sdw_modify_slave_status: initializing completion for Slave 1
[   58.470351] soundwire sdw-master-0: Slave attached, programming device number
[   58.470515] soundwire sdw-master-0: SDW Slave Addr: 30025d071101
[   58.470517] soundwire sdw-master-0: SDW Slave class_id 1, part_id 711, mfg_id 25d, unique_id 0, version 3
[   58.470518] soundwire sdw-master-0: Slave already registered, reusing dev_num:1
[   58.470751] soundwire sdw-master-0: No more devices to enumerate
[   58.470777] rt711-sdca sdw:0:25d:711:1: sdw_modify_slave_status: signaling completion for Slave 1
[   58.470968] rt711-sdca sdw:0:25d:711:1: Configured bus base 1, scale 3, mclk 19200000, curr_freq 4800000
[   58.471105] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_update_status status 1
[   58.471218] rt711-sdca sdw:0:25d:711:1: bard: sdw_handle_slave_status initialization_complete complete
[   58.471247] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_dev_resume
[   58.471449] rt711-sdca sdw:0:25d:711:1: bard: sdw_handle_port_interrupt port 0
[   58.471634] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_interrupt_callback
[   58.471635] rt711-sdca sdw:0:25d:711:1: rt711_sdca_interrupt_callback control_port_stat=4, sdca_cascade=1
[   58.472414] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_update_status status 2
[   58.472439] rt711-sdca sdw:0:25d:711:1: bard: rt711_sdca_update_status status 1
[   58.503570] rt711-sdca sdw:0:25d:711:1: rt711_sdca_headset_detect, detected_mode=0x0
[   58.503578] rt711-sdca sdw:0:25d:711:1: in rt711_sdca_jack_detect_handler, jack_type=0x0
[   58.503581] rt711-sdca sdw:0:25d:711:1: in rt711_sdca_jack_detect_handler, btn_type=0x0
[   58.503584] rt711-sdca sdw:0:25d:711:1: in rt711_sdca_jack_detect_handler, scp_sdca_stat1=0x1, scp_sdca_stat2=0x0
[   58.512061] rt711-sdca sdw:0:25d:711:1: rt711_sdca_pcm_hw_params rt711-sdca-aif1

I thought about your suggestion to use no_pm routines from regmap, but we don't really control when regmap will be used.
For example, if you play on the headset link, but the speaker is suspended. Can we guarantee that the register access through regmap will never result in a bus access?

@plbossart
plbossart requested a review from lgirdwood as a code owner October 28, 2020 21:59
@plbossart

plbossart commented Oct 28, 2020 •

Copy link
Copy Markdown
Member Author

@bardliao indeed I could reproduce the timeout you mentioned with RT711-sdca (TGL device). I don't get how my tests worked on CML and fail miserably on TGL.

That said, with your patch applied instead I can block the device on a capture test, run-all-tests does not complete. Completely stuck, nothing happens in dmesg and I have to power cycle to reboot.

Can you try this with the updated branch?

export TPLG=/lib/firmware/intel/sof-tplg/sof-tgl-rt711-rt1316-rt714.tplg 
/root/sof-test/test-case/check-capture.sh -d 3 -l 1 -r 1

Also if you have a branch where all the tests pass, please share it, I am curious as to what happens here.

@plbossart

Copy link
Copy Markdown
Member Author

trace before the device is stuck:

/root/sof-test/test-case/check-capture.sh -d 3 -l 1 -r 1
2020-10-28 22:06:51 UTC [INFO] Starting /usr/local/bin/sof-logger -t -l /etc/sof/sof-tgl.ldc -o /root/sof-test/logs/check-capture/2020-10-28-17:06:51-1329/slogger.txt
2020-10-28 22:06:51 UTC [INFO] /root/sof-test/test-case/check-capture.sh will use topology /lib/firmware/intel/sof-tplg/sof-tgl-rt711-rt1316-rt714.tplg to run the test case
2020-10-28 22:06:51 UTC [INFO] Pipeline list to ignore is specified, will ignore 'pcm=HDA Digital' in test case
2020-10-28 22:06:51 UTC [INFO] Run command to get pipeline parameters
2020-10-28 22:06:51 UTC [COMMAND] sof-tplgreader.py /lib/firmware/intel/sof-tplg/sof-tgl-rt711-rt1316-rt714.tplg -f 'type:capture & id:any' -b ' pcm:HDA Digital' -s 0 -e
2020-10-28 22:06:51 UTC [INFO] ===== Testing: (Round: 1/1) (PCM: Headset mic [hw:0,1]<capture>) (Loop: 1/1) =====
2020-10-28 22:06:51 UTC [INFO] no file prefix, use /dev/null as dummy capture output
2020-10-28 22:06:51 UTC [COMMAND] arecord -Dhw:0,1 -r 48000 -c 2 -f S16_LE -d 3 /dev/null -v -q
Hardware PCM card 0 'sof-soundwire' device 1 subdevice 0
Its setup is:
  stream       : CAPTURE
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16384
  period_size  : 4096
  period_time  : 85333
  tstamp_mode  : NONE
  tstamp_type  : MONOTONIC
  period_step  : 1
  avail_min    : 4096
  period_event : 0
  start_threshold  : 1
  stop_threshold   : 16384
  silence_threshold: 0
  silence_size : 0
  boundary     : 4611686018427387904
  appl_ptr     : 0
  hw_ptr       : 0
2020-10-28 22:06:56 UTC [INFO] ===== Testing: (Round: 1/1) (PCM: Microphones [hw:0,4]<capture>) (Loop: 1/1) =====
2020-10-28 22:06:56 UTC [INFO] no file prefix, use /dev/null as dummy capture output
2020-10-28 22:06:56 UTC [COMMAND] arecord -Dhw:0,4 -r 48000 -c 2 -f S16_LE -d 3 /dev/null -v -q

@bardliao

Copy link
Copy Markdown
Collaborator

Can you try this with the updated branch?

export TPLG=/lib/firmware/intel/sof-tplg/sof-tgl-rt711-rt1316-rt714.tplg 
/root/sof-test/test-case/check-capture.sh -d 3 -l 1 -r 1

@plbossart I can reproduce the issue on my side. And I can get the error log after a while later.

[  369.694018] Call Trace:
[  369.694030]  __schedule+0x3ba/0x7d0
[  369.694037]  schedule+0x50/0xc0
[  369.694043]  rpm_resume+0x104/0x780
[  369.694051]  ? wait_woken+0xa0/0xa0
[  369.694057]  __pm_runtime_resume+0x42/0x80
[  369.694068]  sdw_nwrite+0x27/0xa0 [soundwire_bus]
[  369.694076]  sdw_write+0x17/0x20 [soundwire_bus]
[  369.694082]  regmap_sdw_mbq_write+0x1e/0x40 [regmap_sdw_mbq]

dmesg.txt

I think we missed the _no_pm change in regmap-sdw-mbq.c

regmap-sdw-mbq.patch
--- a/drivers/base/regmap/regmap-sdw-mbq.c
+++ b/drivers/base/regmap/regmap-sdw-mbq.c
diff --git a/drivers/base/regmap/regmap-sdw-mbq.c b/drivers/base/regmap/regmap-s
dw-mbq.c
index 8ce30650b97c..6675c3a4b829 100644
--- a/drivers/base/regmap/regmap-sdw-mbq.c
+++ b/drivers/base/regmap/regmap-sdw-mbq.c
@@ -15,11 +15,11 @@ static int regmap_sdw_mbq_write(void *context, unsigned int
reg, unsigned int va
        struct sdw_slave *slave = dev_to_sdw_dev(dev);
        int ret;

-       ret = sdw_write(slave, SDW_SDCA_MBQ_CTL(reg), (val >> 8) & 0xff);
+       ret = sdw_write_no_pm(slave, SDW_SDCA_MBQ_CTL(reg), (val >> 8) & 0xff);
        if (ret < 0)
                return ret;

-       return sdw_write(slave, reg, val & 0xff);
+       return sdw_write_no_pm(slave, reg, val & 0xff);
 }

 static int regmap_sdw_mbq_read(void *context, unsigned int reg, unsigned int *val)
@@ -29,11 +29,11 @@ static int regmap_sdw_mbq_read(void *context, unsigned int reg, unsigned int *va
        int read0;
        int read1;

-       read0 = sdw_read(slave, reg);
+       read0 = sdw_read_no_pm(slave, reg);
        if (read0 < 0)
                return read0;

-       read1 = sdw_read(slave, SDW_SDCA_MBQ_CTL(reg));
+       read1 = sdw_read_no_pm(slave, SDW_SDCA_MBQ_CTL(reg));
        if (read1 < 0)
                return read1;

Also if you have a branch where all the tests pass, please share it, I am curious as to what happens here.

I don't have it right now. But I am running a full test with this PR plus my regmap_sdw_mbq change.

@bardliao

Copy link
Copy Markdown
Collaborator

Also if you have a branch where all the tests pass, please share it, I am curious as to what happens here.

I finished the run all test on my TGL rt711-sdca device and found no regression.

@bardliao

Copy link
Copy Markdown
Collaborator

We can always wake up the slave before calling regmap if needed.

How? we don't control when regmap is used, and it can be used in the init_io routine, in the regular control updates, or in the resume operaration.

I meant something like

pm_runtime_get_sync(&slave->dev);
regmap_write(rt711->regmap, SDW_SCP_SDCA_INTMASK1, 0);

For example, if rt711-sdca needs to resume the codec before reading SDW_SCP_SDCA_INTMASK, we can add
pm_runtime_get_sync(&slave->dev);
before

regmap_write(rt711->regmap, SDW_SCP_SDCA_INTMASK1,
                                SDW_SCP_SDCA_INTMASK_SDCA_0);

@plbossart

Copy link
Copy Markdown
Member Author

We can always wake up the slave before calling regmap if needed.

How? we don't control when regmap is used, and it can be used in the init_io routine, in the regular control updates, or in the resume operaration.

I meant something like

pm_runtime_get_sync(&slave->dev);
regmap_write(rt711->regmap, SDW_SCP_SDCA_INTMASK1, 0);

For example, if rt711-sdca needs to resume the codec before reading SDW_SCP_SDCA_INTMASK, we can add
pm_runtime_get_sync(&slave->dev);
before

regmap_write(rt711->regmap, SDW_SCP_SDCA_INTMASK1,
                                SDW_SCP_SDCA_INTMASK_SDCA_0);

ok, got it now, I thought you meant something inside of the regmap functions.

@plbossart

Copy link
Copy Markdown
Member Author

I think we missed the _no_pm change in regmap-sdw-mbq.c

ack, I didn't see this warning so missed the root cause. Good catch @bardliao, re-testing now with your fix.

Comment thread drivers/base/regmap/regmap-sdw-mbq.c Resolved
kv2019i
kv2019i previously approved these changes Nov 2, 2020

@kv2019i kv2019i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor notes, but seems good otherwise. CI results seem fine as well.

Comment thread drivers/soundwire/master.c Resolved
Comment thread drivers/soundwire/bus.c Resolved
plbossart and others added 7 commits November 2, 2020 11:29
The 'master' device acts as a glue layer used during bus
initialization only, and it needs to be 'transparent' for pm_runtime
management. Its behavior should be that it becomes active when one of
its children becomes active, and suspends when all of its children are
suspended.

In our tests on Intel platforms, we routinely see these sort of
warnings on the initial boot:

[ 21.447345] rt715 sdw:3:25d:715:0: runtime PM trying to activate
child device sdw:3:25d:715:0 but parent (sdw-master-3) is not active

This is root-caused to a missing setup to make the device 'active' on
probe. Since we don't want the device to remain active forever after
the probe, the autosuspend configuration is also enabled at the end of
the probe - the device will actually autosuspend only in the case
where there are no devices physically attached. In practice, the
master device will suspend when all its children are no longer active.

Fixes: bd84256 ('soundwire: master: enable pm runtime')
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
When a Slave device is resumed, it may resume the bus and restart the
enumeration. During that process, we absolutely don't want to call
regular read/write routines which will wait for the resume to
complete, otherwise a deadlock occurs.

Fixes: 60ee9be ('soundwire: bus: add PM/no-PM versions of read/write functions')
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
When a Slave device is resumed, it may resume the bus and restart the
enumeration. During that process, we absolutely don't want to call
regular read/write routines which will wait for the resume to
complete, otherwise a deadlock occurs.

This patch fixes the same problem as the previous one, but is split to
make the life of linux-stable maintainers less painful.

Fixes: 29d158f ('soundwire: bus: initialize bus clock base and scale registers')
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
There is no need to play with pm_runtime reference counts, if needed
the codec drivers are already explicitly resumed.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
sdw_update_slave_status will be invoked when a codec is attached,
and the codec driver will initialize the codec with regmap functions
while the codec device is pm_runtime suspended.

regmap routines currently rely on regular SoundWire IO functions,
which will call pm_runtime_get_sync()/put_autosuspend.

This causes a deadlock where the resume routine waits for an
initialization complete signal that while the initialization complete
can only be reached when the resume completes.

The only solution if we allow regmap functions to be used in resume
operations as well as during codec initialization is to use _no_pm
routines. The duty of making sure the bus is operational needs to be
handled above the regmap level.

Fixes: 7c22ce6 ('regmap: Add SoundWire bus support')
Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Use no_pm versions for write and read.

Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Intel stress-tests routinely report IO timeouts and invalid power
management transitions. Upon further analysis, we seem to be using the
wrong devices in pm_runtime calls.

Before reading and writing registers, we first need to make sure the
Slave is fully resumed. The existing code attempts to do such that,
however because of a confusion dating from 2017 and copy/paste, we
end-up resuming the parent only instead of resuming the codec device.

This can lead to accesses to the Slave registers while the bus is
still being configured and the Slave not enumerated, and as a result
IO errors occur.

This is a classic problem, similar confusions happened for HDaudio
between bus and codec device, leading to power management issues.

Fix by using the relevant device for all uses of pm_runtime functions.

Fixes: 60ee9be ('soundwire: bus: add PM/no-PM versions of read/write functions')
Fixes: aa79293 ('soundwire: bus: fix io error when processing alert event')
Fixes: 9d715fa ('soundwire: Add IO transfer')
Reported-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
@plbossart

Copy link
Copy Markdown
Member Author

@naveen-manohar @sathya-nujella @RDharageswari this PR does not seem to introduce any regressions so far. The additional tests on CML_RVP, TGL_Chromebook and 2 other devices seem fine.
Could we please ask you to give this a try and see if this helps with all the suspend/resume issues you reported?

kv2019i
kv2019i previously approved these changes Nov 2, 2020
Comment thread drivers/soundwire/bus.c Outdated
ret = sdw_update(slave, SDW_DP0_INTMASK, val, val);
ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
if (ret < 0)
dev_err(slave->bus->dev,

@RanderWang RanderWang Nov 3, 2020 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart should we change it to "dev_err(slave->dev,". I see we use slave->dev now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right @RanderWang we should also scan the dev_err/dbg log to see which device is used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, can you recheck @RanderWang and @bardliao ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, can you recheck @RanderWang and @bardliao ?

@plbossart I checked bus.c. I think you didn't miss anything. But I didn't get why do we use bus->dev in sdw_modify_slave_status?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao there is only one case with dev_vdbg where we use bus->dev. I wanted to have one bus reference here that shows a bus-level evolution in terms of attachment, so that we can filter it out. It was really more to post-process the results than a functional change.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao there is only one case with dev_vdbg where we use bus->dev. I wanted to have one bus reference here that shows a bus-level evolution in terms of attachment, so that we can filter it out. It was really more to post-process the results than a functional change.

ok. got it. Thaks.

The SoundWire bus code confuses bus and Slave device levels for
dev_err/dbg logs. That's not impacting functionality but the accuracy
of kernel logs.

We should only use bus->dev for bus-level operations and handling of
Device0. For all other logs where the device number is not zero, we
should use &slave->dev to provide more precisions to the
user/integrator.

Reported-by: Rander Wang <rander.wang@linux.intel.com>
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>

@RanderWang RanderWang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on TGL RVP and volteer

@plbossart

Copy link
Copy Markdown
Member Author

I'll go ahead and merge this, it was reviewed multiple times and we need to start stress testing this code. So far it does not add any regressions, let's see if this actually fixes some of the timeout problems as we saw in early manual tests.

@plbossart
plbossart merged commit bfc71b9 into thesofproject:topic/sof-dev Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants